<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/15
 * Time: 18:05
 */

namespace Webview\Controller;


use Admin\Api\MessageApi;
use Admin\Api\PostApi;
use Admin\Api\VPostInfoApi;
use Common\Api\AccountApi;
use Shop\Api\MemberConfigApi;
use Shop\Api\RedEnvelopeApi;
use Ucenter\Api\MemberApi;
use Ucenter\Api\MemberExpApi;
use Webview\Api\SigninApi;

class IndexController extends WebViewController{

    protected function _initialize(){
        parent::_initialize();
    }

    public function signIn(){

        $signIn = I('get.signIn',0);
        $this->assign('signIn',$signIn);
        if($signIn==1){
            //签到加分
            $score = 0;
            $result = apiCall(SigninApi::SIGN,array($this -> uid));
            if($result['status']){
                $score = $result['info'];
            }else{
                $this -> error($result['info']);
            }
            $result = apiCall(MemberApi::RAISE_SCORE,array($this -> uid,$score));
            if(!$result['status']){
                $this -> error($result['info']);
            }

        }

        $map = array(
            'uid' => $this -> uid,
        );

        $result = apiCall(AccountApi::GET_INFO,array($this -> uid));

        if($result['status']){
            $this -> assign('score',$result['info']['score']);
            $this -> assign('nickname',$result['info']['nickname']);
        }else{
            $this -> error($result['info']);
        }

        $result = apiCall(SigninApi::GET_INFO,array($map));
        if($result['status']){
            $continues_signin = $result['info']['continues_signin'];
            if(empty($continues_signin)){
                $continues_signin = 0;
            }else{
                $continues_signin = $continues_signin - $signIn;
            }
            $res1 = $this -> judgeYesterday(time(),$result['info']['sign_in_time']);
            $res2 = $this -> judgeToday(time(),$result['info']['sign_in_time']);
            if($res1 != true && $res2 != true){
                $continues_signin = 0;
            }
            $this -> assign('continues_signin',$continues_signin);

            $tmp1 = date("Y-m-d",$result['info']['sign_in_time']);
            $tmp2 = date("Y-m-d",time());
            $this->assign('is_sign',$tmp1==$tmp2?1:0);

        }else{
            $this -> error($result['info']);
        }

        $this -> display();


    }

    public function gainScore(){

        $this -> display();

    }

    /**
     * 废弃，移至PostController
     */
    private function viewPost(){
        $id = I('get.id',0);
        $map = array('id'=>$id);
        $result = apiCall(VPostInfoApi::GET_INFO,array($map));
        //dump($result);
        if(!$result['status']){
            $this->error($result['info']);
        }

        if(!$result['info']){
            $this->error('找不到相关内容!');
        }

        $content = htmlspecialchars_decode($result['info']['post_content']);
        $title = $result['info']['post_title'];
        $this->assign("post",$result['info']);
        $this->assign("title",$title);
        $this->assign("content",$content);

        //@author 老胖子-何必都 <hebiduhebi@126.com>
        //查看次数+1
        $result = apiCall(PostApi::SET_INC,array(array('id'=>$id),'post_view',1));

        $this->display();
    }

    public function assert(){
        $result = apiCall(AccountApi::GET_INFO,array($this -> uid));
        $score = "";
        if($result['status']){
            $score = $result['info']['score'];
        }else{
            $this -> error($result['info']);
        }
        $rank = apiCall(MemberExpApi::GET_USER_RANK,array($this -> uid));
        if($rank['status']){
            $rank = $rank['info']['rank'];
        }else{
            $this -> error($rank['info']);
        }
        $this -> assign('score',$score);
        $this -> assign('rank',$rank);

        $map = array(
            'uid' => $this -> uid,
            'use_status' => 0,
        );
        $time_map = "`get_time`+`expire_time`>".time();

        $result = apiCall(RedEnvelopeApi::USEFUL_COUNT,array($map,$time_map));
        if($result['status']){
            $this -> assign('red_num',$result['info']);
        }else{
            $this -> error($result['info']);
        }

        $this -> display();

    }


    public function invitationAward(){
        //取邀请码
        $idcode = "";
        $map = array(
            'uid' => $this -> uid,
        );
        $result = apiCall(MemberConfigApi::GET_INFO,array($map));
        if($result['status']){
            $idcode = $result['info']['idcode'];
        }else{
            $this -> error($result['info']);
        }
        if($idcode == ""){
            $this -> error('参数错误');
        }
        $this -> assign('idcode',$idcode);
        //邀请记录暂时不需要
        /*$map = array(
            "itboye_member_config.invite_id" => $this -> uid,
        );
        $page = array(
            'curpage'=>1,
            'size'=>20,
        );
        $order = "itboye_ucenter_member.reg_time desc";
        $field = "itboye_ucenter_member.id as id,itboye_ucenter_member.username as username,itboye_ucenter_member.reg_time as reg_time";
        $field = $field.",common_member.nickname as nickname";
        $result = apiCall(MemberConfigApi::QUERY_ACCOUNT,array($map,$page,$order,false,$field));
        if($result['status']){
            $list = $result['info']['list'];
        }else{
            $this -> error($result['info']);
        }
        $this -> assign('list',$list);*/
        $this -> display();
    }

    public function systemMessage(){

        $msg_id = I('get.id',0);
        $result = apiCall(MessageApi::GET_INFO,array(array('id'=>$msg_id)));
        if($result['status']){
            $this->assign('content',htmlspecialchars_decode($result['info']['content']));
        }
        $this->display();

    }

    private function judgeToday($time1,$time2){
        $tmp1 = date("Y-m-d",$time1);
        $tmp2 = date("Y-m-d",$time2);
        return $tmp1 == $tmp2;
    }

    private function judgeYesterday($time1,$time2){
        $tmp1 = getdate($time1);
        $tmp2 = getdate($time2);
        if($tmp1['year'] == $tmp2['year'] && $tmp1['mon'] == $tmp2['mon'] && ($tmp1['mday']-$tmp2['mday']==1)){
            return true;
        }else{
            return false;
        }
    }
}